﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using HARTCalibrationTool.JsonFilesManager;

namespace HARTCalibrationTool
{
    public class DataInfo
    {
        public string Transducer { set; get; }
        public bool Status { set; get; }
        public string SN { set; get; }
        public Dictionary<string, string[]> Data { set; get; }
    }

    public class JsonFileManager : JsonBase
    {
        private JsonDevsInfo _jsonDevsInfo;

        public JsonFileManager(JsonDevsInfo jsonDevsInfo) 
        { 
            _jsonDevsInfo = jsonDevsInfo;
        }

        public bool JsonFileSaveInfo(string fileName)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();

            var settings = new JsonSerializerSettings
            {
                Formatting = Formatting.Indented,
                NullValueHandling = NullValueHandling.Include,
            };

            var jsonString = JsonConvert.SerializeObject(_jsonDevsInfo, settings);

            fileDialog.FileName = fileName;
            fileDialog.Filter = "JSON file (*.json) | *.json";
            fileDialog.InitialDirectory = initialDirectory;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                initialDirectory = fileDialog.FileName;

                File.WriteAllText(initialDirectory, jsonString);

                return true;
            }
            else
            {
                return false;
            }
        }

        public static JsonDevsInfo JsonLoadInfo(string filePath)
        {
            string jsonString = File.ReadAllText(filePath);

            JsonDevsInfo jsonDevsInfo = JsonConvert.DeserializeObject<JsonDevsInfo>(jsonString);

            return jsonDevsInfo;
        }

        public void JsonFileModifyTransducerInfo(byte transducerID, string serialNum)
        {
            _jsonDevsInfo.dataInfo[transducerID - 1].Status = true;
            _jsonDevsInfo.dataInfo[transducerID - 1].SN = serialNum;
        }

        public void JsonFileModifyData(byte transducerID, string type, string[] data)
        {
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][0] = data[0];
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][1] = data[1];
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][2] = data[2];
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][3] = data[3];
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][4] = data[4];
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][5] = data[5];
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][6] = data[6];
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][7] = data[7];
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][8] = data[8];
            _jsonDevsInfo.dataInfo[transducerID - 1].Data[type][9] = data[9];
        }
    }

    public class JsonDevsInfo
    {
        [JsonProperty(Order = 1)]
        public string OrderSerialNumber { get; set; }

        [JsonProperty(Order = 2)]
        public string TemperaturePoint { get; set; }

        [JsonProperty(Order = 3)]
        public int RangeUp { get; set; }

        [JsonProperty(Order = 4)]
        public int RangeDown { get; set; }

        [JsonProperty(Order = 5)]
        public uint EqualPoints { get; set; }

        [JsonProperty(Order = 6)]
        public string CurrentPressure { get; set; }

        [JsonProperty(Order = 7)]
        public DataInfo[] dataInfo;

        public JsonDevsInfo()
        {
            dataInfo = new DataInfo[32]
            {
                new DataInfo {
                    Transducer = "变送器1",
                    Status = false, 
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器2",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器3",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器4",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器5",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器6",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器7",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器8",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器9",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器10",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器11",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器12",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器13",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器14",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器15",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器16",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器17",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器18",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器19",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器20",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器21",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器22",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器23",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器24",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器25",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器26",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器27",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器28",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器29",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器30",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器31",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                },
                new DataInfo {
                    Transducer = "变送器32",
                    Status = false,
                    SN = null,
                    Data = new Dictionary<string, string[]>
                    {
                        { "压力(上行)", new string[10] },
                        { "温度(上行)", new string[10] },
                        { "压力(下行)", new string[10] },
                        { "温度(下行)", new string[10] },
                    }
                }
            };
        }
    }
}
